package start.spring.basic.util.common;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.springframework.util.StreamUtils;

/**
 * 
 * ****************************************************************************
 * http增强类，用于解决request body无法多次读取问题
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2021年8月5日
 ******************************************************************************
 */
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {

	//保存流
	  private byte[] requestBody = null;
	  
	  public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
	    super(request);
	    requestBody = StreamUtils.copyToByteArray(request.getInputStream());
	  }
	  
	  @Override
	  public ServletInputStream getInputStream() throws IOException {
	  
	    final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
	  
	    return new ServletInputStream() {
	  
	      @Override
	      public int read() throws IOException {
	        return bais.read();
	      }
	  
	      @Override
	      public boolean isFinished() {
	        return false;
	      }
	  
	      @Override
	      public boolean isReady() {
	        return false;
	      }
	  
	      @Override
	      public void setReadListener(ReadListener readListener) {
	  
	      }
	    };
	  }
	  
	  @Override
	  public BufferedReader getReader() throws IOException{
	    return new BufferedReader(new InputStreamReader(getInputStream()));
	  }
}
